<template name="Notice">
	<div>
		<div v-if="NoticeType === '1'">
			<u-mask :show="NoticeShow" :mask-click-able="false" z-index="9999" mask-click-able="true" @click="closeNotice">
				<div class="mask-solt">
					<div style="width: 550rpx;">
						<u-swiper :list="NoticeDataList" name="noticeImage" height="840" img-mode="aspectFit" bg-color="transparent" @click="jumpNotice"></u-swiper>
					</div>
				</div>
			</u-mask>
		</div>
		<div v-if="NoticeType === '0'">
			<u-modal v-model="NoticeShow" :title="NoticeTitle" :confirm-text="NoticeConfirmText"
				   @confirm="redirectToUrl(JumpAddress,addressSource)"
				   confirm-color="#e55e97" :mask-close-able="true">
				<view class="slot-content">
					<u-parse v-if="NoticeType === '0'" :html="escape2Html(escapeBR(NoticeContent))"></u-parse>
				</view>
			</u-modal>
		</div>
	</div>
</template>

<script>
import globalData from "@/globalData";

export default {
	name: "Notice",
	data() {
		return {
			NoticeShow: false,//每日公告显示
			NoticeTitle: "",
			NoticeContent: "",
			NoticeType: '',// 每日公告类型 0 文字公告 1图片公告
			NoticeImage: '',
			JumpAddress: '',
			addressSource: '',
			NoticeConfirmText: '已知晓',
			NoticeDataList: []
		};
	},

  methods: {

	/**
	 * 判断是否显示每日公告
	 */
	isShowNotice() {
		console.log("执行了isShowNotice()")
		//首先获取是否执行过
		let self = this;
		uni.getStorage({
			key: 'NoticeToday',
			success: function (res) {
				//成功的话 说明之前执行过，再判断时间是否是当天
				if (res.data && res.data != new Date().toLocaleDateString()) {
					self.getNoticeData()
				}else{
					self.updateNoticeData()
				}
			},
			fail: function (res) {
				self.getNoticeData()
			}
		})
	},
	
	//关闭公告模态框
	closeNotice(e) {
	  this.NoticeShow = false;
	},
	
	/**
	 * 获取公告数据
	 */
	async getNoticeData() {
	  let result = await this.$u.api.unifyMiniRest({
		systemid: "meily",
		url: "/notice/last",
		pageNum: 1,
		pageSize: 50,
		loading: false
	  });
	  if (result.code == 200) {
		if (this.isNotNull(result.rows) && result.rows.length > 0) {
			uni.setStorageSync("NoticeToday", new Date().toLocaleDateString())
			uni.setStorageSync("NoticeList", result.rows)
			this.updateNoticeData()
		}
	  }
	},
	
	/** 
	 * 轮播图公告点击跳转
	 */
	jumpNotice(index) {
		this.routeAD(this.NoticeDataList[index].jumpAddress, 'applet')
	},
	
	/** 
	 * 更新该页面公告弹出的次数
	 */
	updateNoticeData(){
		let _this = this
		const pages = getCurrentPages() // 获取栈实例
		let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
		uni.getStorage({
			key: 'NoticeList',
			success: function (res) {
				let NoticeList = res.data
				for(const i in NoticeList){
					if(NoticeList[i].usedPages == currentRoute){
						let obj = NoticeList[i]
						try {
							let contentList = JSON.parse(obj.content)
							_this.NoticeDataList = contentList
							_this.NoticeTitle = obj.title
							_this.NoticeContent = contentList[0].contentText
							_this.NoticeType = obj.type
							_this.NoticeImage = contentList[0].noticeImage
							_this.JumpAddress = contentList[0].jumpAddress
							_this.addressSource = obj.addressSource
						} catch (e) {
							_this.NoticeTitle = obj.title
							_this.NoticeContent = obj.content
							_this.NoticeType = obj.type
							_this.NoticeImage = obj.noticeImage
							_this.JumpAddress = obj.jumpAddress
							_this.addressSource = obj.addressSource
						}
						if (_this.JumpAddress) {
							_this.NoticeConfirmText = '前往查看';
						}
						let openTimes = NoticeList[i].openTimes
						if(_this.$u.test.isEmpty(openTimes) || openTimes<NoticeList[i].ejectNumTimes){//如果打开次数为空或者小于允许打开的次数，则给次数+1，并存回缓存中，最后打开公告
							if(_this.$u.test.isEmpty(openTimes)){
								NoticeList[i].openTimes = 1
							}else{
								NoticeList[i].openTimes = parseInt(openTimes) + 1
							}
							uni.setStorageSync("NoticeList", NoticeList)
							_this.NoticeShow = true
						}
						break
					}
				}
			}
		});
	}

  },
};
</script>

<style scoped>
.mask-solt {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
</style>
